package cn.wolfcode.demo;


import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.apache.commons.io.IOUtils;
import org.junit.Test;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class demo2 {
    @Test
    public void testDemo(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        System.out.println(processEngine);
    }
    @Test
    public void testDeploy(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deployment = repositoryService.createDeployment().addClasspathResource("bpnm/leave.bpmn")
                .addClasspathResource("bpnm/leave.png")
                .name("请假流程").deploy();
        System.out.println(deployment.getId() + "流程部署ID");
        System.out.println("流程部署名称" + deployment.getName());
    }
    @Test
    public void testDefinitionQuery(){
        //启动引擎
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //流程定义对象，当部署成功以后就存在 ，跟资源有关-repositoryService
        RepositoryService repositoryService = processEngine.getRepositoryService();
        List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery()
                .processDefinitionKey("leave")
                //  .latestVersion()//最新的版本
                // .orderByProcessDefinitionVersion()//按照版本排序（拿全部）
                //.singleResult()//单个
                .list();
        for (ProcessDefinition definition:list){
            System.out.println("流程定义ID:"+definition.getId());
            System.out.println("流程定义名称:"+definition.getName());
            System.out.println("流程定义key:"+definition.getKey());
            System.out.println("流程定义版本:"+definition.getVersion());
            System.out.println("流程部署ID:"+definition.getDeploymentId());
            System.out.println("====================");
        }
    }
//下载流程图文件文件和流程图片
    @Test
    public void testDownloadResource() throws IOException {
        //启动引擎
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //流程定义对象，当部署成功以后就存在 ，跟资源有关-repositoryService
        RepositoryService repositoryService = processEngine.getRepositoryService();
        //下载资源需要两个参数：部署id和资源名称，2个参数都可以流程定义对象中找到ProcessDefinition
        //所以下载资源前先查询流程定义对象ProcessDefinition
        ProcessDefinition processDefinition = repositoryService
                .createProcessDefinitionQuery()
                .processDefinitionKey("leave").latestVersion().singleResult();
        //.orderByProcessDefinitionVersion()//按照版本排序
               // .desc()//降序
               // .list();
       //流程文件
        InputStream bpmnInput = repositoryService.getResourceAsStream(processDefinition.getDeploymentId(), processDefinition.getResourceName());
    //图片
        InputStream pngInput = repositoryService.getResourceAsStream(
                processDefinition.getDeploymentId(),
                processDefinition.getDiagramResourceName());
        //设置bpmn输入
        FileOutputStream bpmnOutPut = new FileOutputStream("D:/leave.bpmn");
        //设置png输入
        FileOutputStream pngOutPut = new FileOutputStream("D:/leave.png");
        IOUtils.copy(bpmnInput,bpmnOutPut);
        IOUtils.copy(pngInput,pngOutPut);
    }
    //流程定义删除，删除部署对象之后，自动删除流程定义对象
    @Test
    public void testDeleteDeploy(){
        //启动引擎
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //流程定义对象，当部署成功以后就存在 ，跟资源有关-repositoryService
        RepositoryService repositoryService = processEngine.getRepositoryService();
   //删除部署对象
      //  repositoryService.deleteDeployment("10001");
        //如果流程在启动中，是无法直接删除，会报有外键引用，如果执意删除，可以使用级联删除
        repositoryService.deleteDeployment("1",true);

    }
    @Test
    public void testStartProcess(){
        Long id=1L;
        //启动引擎
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //流程定义对象，当部署成功以后就存在 ，跟资源有关-repositoryService
        RuntimeService runtimeService = processEngine.getRuntimeService();
        ProcessInstance instance = runtimeService.startProcessInstanceByKey("leave",id.toString());
        System.out.println(instance.getProcessDefinitionId()+"流程定义的id");
        System.out.println("流程实例id" + instance.getId());
        System.out.println("businessKyt=" + instance.getBusinessKey());
    }
    @Test
    public void testBusinessKey(){
        //启动引擎
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //流程定义对象，当部署成功以后就存在 ，跟资源有关-repositoryService
        TaskService taskService = processEngine.getTaskService();
        RuntimeService runtimeService = processEngine.getRuntimeService();
        List<Task> list = taskService.createTaskQuery().processDefinitionKey("leave").taskAssignee("李四").list();
        for (Task task : list) {
            System.out.println(task.getProcessDefinitionId() + "流程定义id");
            System.out.println("流程实例Id" + task.getProcessInstanceId());
            System.out.println("任务id" + task.getId());
            System.out.println("任务名称" + task.getName());
            ProcessInstance instance = runtimeService.createProcessInstanceQuery()
                    .processInstanceId(task.getProcessInstanceId()).singleResult();

            System.out.println("任务key" + instance.getBusinessKey());
        }
    }
    @Test
    public void testSuspendAllProcessInstance(){
        //启动引擎
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //流程定义对象，当部署成功以后就存在 ，跟资源有关-repositoryService
        RepositoryService repositoryService = processEngine.getRepositoryService();
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionKey("leave").singleResult();
        boolean suspended = processDefinition.isSuspended();
        String processDefinitionId = processDefinition.getId();
        if (suspended) {
            repositoryService.activateProcessDefinitionById(processDefinitionId,true,null);
            System.out.println("流程ID"+processDefinitionId+"已激活");
        }else{
            repositoryService.suspendProcessDefinitionById(processDefinitionId,true,null);
            System.out.println("流程ID"+processDefinitionId+"已挂起");
        }

    }
    @Test
    public void testSuspendStatus(){
        //启动引擎
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //流程定义对象，当部署成功以后就存在 ，跟资源有关-repositoryService
        TaskService taskService = processEngine.getTaskService();
        List<Task> list = taskService.createTaskQuery().processDefinitionKey("leave").taskAssignee("张三").list();
        for (Task task : list) {
            System.out.println("流程定义id = " + task.getProcessDefinitionId());
            System.out.println("流程实例id = " + task.getProcessInstanceId());
            System.out.println("任务id = " + task.getId());
            System.out.println("任务名称 = " + task.getName());
            System.out.println("任务状态:"+(task.isSuspended()?"已挂起":"已激活"));
        }
    }
    @Test
    public void testQueryProcessInstance(){
        //创建ProcessEngine对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //获取RepositoryService
        RuntimeService runtimeService = processEngine.getRuntimeService();
        List<ProcessInstance> processInstanceList = runtimeService
                .createProcessInstanceQuery()
                .processDefinitionKey("leave")
                .list();
        for(ProcessInstance processInstance:processInstanceList){
            System.out.println("流程实例Id:"+processInstance.getId()+",状态:"+(processInstance.isSuspended()?"已挂起":"已激活"));
        }
    }
    @Test
    public void testSuspendSingleProcessInstance(){
        //流程实例Id
        String processInstanceId = "2501";
        //创建ProcessEngine对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //获取RepositoryService
        RuntimeService runtimeService = processEngine.getRuntimeService();
        //根据流程实例Id获取流程实例对象
        ProcessInstance processInstance = runtimeService
                .createProcessInstanceQuery()
                .processInstanceId(processInstanceId)
                .singleResult();
        //状态
        boolean suspended = processInstance.isSuspended();
        System.out.println("流程实例ID:"+processInstanceId+",状态:"+ (suspended?"已挂起":"已激活"));
        if(suspended){
            runtimeService.activateProcessInstanceById(processInstanceId);
            System.out.println("流程实例ID:"+processInstanceId+",状态修改为已激活");
        }else{
            runtimeService.suspendProcessInstanceById(processInstanceId);
            System.out.println("流程实例ID:"+processInstanceId+",状态修改为已挂起");
        }
    }
}
